package sysmgr.controller;

import sysmgr.bean.Staff;
import sysmgr.service.StaffService;
import sysmgr.service.impl.StaffServicempl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 类的描述：
 *      后端向前端传送json数据的控制器
 *
 * @author  cwn
 * @since 2020/12/24 9:17
 */
public class StaffController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StaffService service=new StaffServicempl();
        String username=req.getParameter("username");
        String pwd = req.getParameter("pwd");
        String url=req.getRequestURI();
        url=url.substring(url.lastIndexOf("/")+1,url.length());
        String strURL=req.getContextPath();
        if(url.equals("staffLogin")){
            //创建staff对象将其注入值
            Staff staff=new Staff();
            staff.setUserName(username);
            staff.setPwd(pwd);
            String login =service.staffLogin(staff);
            if (login == null){
                System.out.println("错误   ");
                req.getSession().setAttribute("loginErro","登录信息不正确");
                resp.sendRedirect(strURL+"/login.jsp");
            }else {
                req.getSession().setAttribute("uesrInfo",staff);
                req.getSession().setAttribute("loginErro","");
                RequestDispatcher requestDispatcher = req.getRequestDispatcher("../" + login);
                requestDispatcher.forward(req, resp);
            }}}}
